package com.aube.plugin.rank.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aube.plugin.rank.ILoginChecker;
import com.aube.plugin.rank.OnRefreshEndListener;
import com.aube.plugin.rank.R;
import com.aube.plugin.rank.model.RankModel;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.DynamicLoaderHelper;
import com.huyn.baseframework.dynamicload.IPositionManager;

/**
 * Created by huyaonan on 16/5/12.
 */
public class ViewPagerRank extends DLLayout {

    private Context context;

    private LinearLayout mLayoutGroup;

    private RankGroup mRankGroup;

    private RankModel mRankModel;

    private ILoginChecker iLoginChecker;

    public ViewPagerRank(Context context) {
        this(context, null);
    }
    public ViewPagerRank(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public ViewPagerRank(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @Override
    public void initViews() {
        inflate(R.layout.vp_rank, this, true);
        mLayoutGroup = (LinearLayout) findViewById(R.id.rank_root);
    }

    public void setLoginChecker(ILoginChecker iLoginChecker) {
        this.iLoginChecker = iLoginChecker;
    }

    @Override
    public void fillData(Object o) {
        if(o == null || !(o instanceof RankModel))
            return;

        mRankModel = (RankModel) o;

        mLayoutGroup.removeAllViews();

        mRankGroup = (RankGroup) DynamicLoaderHelper.loadTargetLayout(context, getDLPluginPackage(), RankGroup.class.getName());
        mLayoutGroup.addView(mRankGroup, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mRankGroup.setDLBridge(mBridge);
        mRankGroup.setLoginChecker(iLoginChecker);
        mRankGroup.fillData(mRankModel, true, true, false);
    }

    public void refresh(RankModel model, OnRefreshEndListener listener) {
        if(mRankGroup != null)
            mRankGroup.refresh(model, false, listener);
    }

    @Override
    public void update(Object data) {
        if(data == null || !(data instanceof RankModel))
            return;

        mRankModel = (RankModel) data;

        if(mRankGroup != null)
            mRankGroup.update(mRankModel.characters);
    }

    @Override
    public void update() {
        if(mRankGroup != null)
            mRankGroup.update();
    }

    @Override
    public void notifyAnim(int animDelay) {
    }

    @Override
    public void registToManager(IPositionManager manager) {
    }

    @Override
    public void unreistFromManager(IPositionManager manager) {
    }

    @Override
    public int[] getLocations() {
        return null;
    }

}

